package arabicScrabble;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.LinkedList;

import javax.swing.*;
import javax.xml.ws.soap.Addressing;

public class Rack extends JPanel{

	public LinkedList<Letter> letters; 
	Bag bag;
	
	Rack(Bag bag){
		letters = new LinkedList<Letter>();
		this.setLayout(new GridLayout(1, 7));
		//this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		this.bag = bag;
		
		fill();

	}
	 private void fill(){
		
	//	printList();
		for(int i = 0; i < 7; i++){
			int x = (int)(Math.random()*bag.getLetters().size());
			addLetter(bag.removeLetter(x));
			//letters.addLetter(bag.removeLetter(x));
		}
		//printList();
	}
		 
	public boolean refill(){
		int size = letters.size();
		boolean flag=true;
		if(size==0)
			flag= false;
		
	//	System.out.println( "size" + size);
		for(int i = 0;bag.getLetters().size()>0 && i < (7-size); i++){
			int x = (int)(Math.random()*bag.getLetters().size());
			addLetter(bag.removeLetter(x));
			
	//		System.out.println("refill" + i);
			
		}
		
	//	repaint();
		//validate();
		return flag;
		
	}
	
	
	public boolean addLetter(Letter arg0) {
	
		if(letters.size()<8){
			letters.addFirst(arg0);
	 super.add(arg0);
	 arg0.setEnabled(true);
	 //this.validate();
	 //ystem.out.println("added  "+arg0.symbol);
		return true;
	}
	//	System.out.println("not added");
		return false;
		
	}
	
	@Override
	public void remove(Component arg0) {
		
		super.remove(arg0);
		arg0.setEnabled(false);
		letters.remove(arg0);
	}
	
	public void removeLetters(LinkedList<Letter> let){
		
			letters.clear();
			removeAll();
			repaint();
		
	}

	public  void printList() {
		System.out.println(" ===================>");
		for (int i = 0; i < letters.size(); i++) {

			System.out.println(//" -> x: " + letters.get(i).getSquare().getI()
					//+ " -> y: " + letters.get(i).getSquare().getJ()
					 " Symbol " + letters.get(i).symbol + " size "
					+ letters.size());
		}
	}

}
